﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace RG.Common
{
    public static class BindingList_Ext
    {
        public static BindingList<T> Union<T>(this BindingList<T> a, BindingList<T> b)
        {
            var res = new BindingList<T>();
            foreach (T val in a)
                res.Add(val);
            foreach (T val in b)
                res.Add(val);
            return res;
        }
    }

    public static class ChunkExtension
    {
        /*
        public static List<List<T>> Chunk<T>(
            this IEnumerable<T> theList,
            int chunkSize)
        {
            return theList
                .Select((x, i) => new
                {
                    data = x,
                    indexgroup = i / chunkSize
                })
                .GroupBy(x => x.indexgroup, x => x.data)
                .Select(g => new List<T>(g)).ToList();
        } */

        public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> enumerable, int groupSize)
        {
            var list = new List<T>(groupSize);

            foreach (var item in enumerable)
            {
                list.Add(item);
                if (list.Count == groupSize)
                {
                    yield return list;

                    list = new List<T>(groupSize);
                }
            }

            if (list.Count != 0)
            {
                yield return list;
            }
        }

    }

    public static class IDictionaryExtension
    {
        public static TValue SafeGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defValue)
        {
            TValue value;
            return dictionary.TryGetValue(key, out value) ? value : defValue;
        }

        public static string SafeGetValue<TKey>(this IDictionary<TKey, string> dictionary, TKey key)
        {
            return dictionary.SafeGetValue(key, key.ToString());
        }

        public static TKey SafeGetKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value, TKey defKey)
        {
            foreach (var pair in dictionary)
                if (pair.Value.Equals(value)) return pair.Key;
            return defKey;
        }
    }

    public static class DelphiHelpers
    {
        public static DateTime FromDelphiDateTime(this double pv)
        {
            return DelphiZeroDate.AddDays(pv);
        }

        public static DateTime FromDelphiDateTime(this decimal pv)
        {
            return FromDelphiDateTime((double)pv);
        }

        public static double ToDelphiDateTimeDbl(this DateTime date)
        {
            return (date - DelphiZeroDate).TotalDays;
        }

        public static decimal ToDelphiDateTime(this DateTime date)
        {
            return (decimal)ToDelphiDateTimeDbl(date);
        }

        public static DateTime DelphiZeroDate = new DateTime(1899, 12, 30);
    }

    public static class DayOfWeek_Ext
    {
        public static readonly string[] WeekDayNames =
            new[]
                {
                    "??", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"
                };
        public static int ToInt(this DayOfWeek w)
        {
            switch (w)
            {
                case DayOfWeek.Monday:
                    return 1;
                case DayOfWeek.Tuesday:
                    return 2;
                case DayOfWeek.Wednesday:
                    return 3;
                case DayOfWeek.Thursday:
                    return 4;
                case DayOfWeek.Friday:
                    return 5;
                case DayOfWeek.Saturday:
                    return 6;
                case DayOfWeek.Sunday:
                    return 7;
                default:
                    throw new ArgumentOutOfRangeException("w");
            }
        }

        public static string ToRussian(this DayOfWeek w)
        {
            return WeekDayNames[w.ToInt()];
        }
    }

    public static class String_Ext
    {
        public static string TrimLength(this string s, int maxlen)
        {
            return (s.Length > maxlen) ? s.Substring(0, maxlen) : s;
        }

        public static string ProtectThePhone(this string phone)
        {
            if (string.IsNullOrEmpty(phone)) return "";
            string firstChar = phone.Substring(0, phone.StartsWith("+") ? 2 : 1);
            return string.Format("{0}-****-{1}", firstChar, phone.Substring(phone.Length - 5, 5));
        }
    }

    public static class MailAddressHelper
    {
        public static string Join(string s1, string s2)
        {
            if (s1 == String.Empty)
                return s2;
            if (s2 == String.Empty)
                return s1;
            return String.Format("{0}, {1}", s1, s2);
        }

        public static string Join(string s1, string s2, string s3)
        {
            return Join(Join(s1, s2), s3);
        }
    }
}
